package com.example.walkingtour.maps;

import java.util.List;

import com.example.walkingtour.R;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.Menu;

/*
 * Displays the map.  
 * 
 * The class, MyMapFragment, calls this activity to appear when the Map View tab is clicked
 */
public class TourMapActivity extends MapActivity {

	private List<Overlay> mMapOverlays;
	private Drawable mDrawable;
	private TourMapItemizedOverlay mOverlay;
	public static double mLatitude, mLongitude;
	private MapView mMapView;

	public static Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tour_map);

		initializeTourMap();
		populateOverlays();
		mMapOverlays.add(mOverlay);
	}

	private void initializeTourMap() {
		mContext = getApplicationContext();

		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setBuiltInZoomControls(true);

		initializeOverlay();
	}

	private void initializeOverlay() {
		mMapOverlays = mMapView.getOverlays();
		mDrawable = this.getResources().getDrawable(R.drawable.androidmarker);
		mOverlay = new TourMapItemizedOverlay(mDrawable, this);
	}

	/*
	 * Adds the mOverlay items (i.e. the android markers) to the map.
	 */
	public void populateOverlays() {
		int mFirstItem = 1; // start at one

		TourMapActivityHelper helper = new TourMapActivityHelper(mOverlay);
		mOverlay = helper.addOverlaysToMap(mFirstItem, getContentResolver());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_tour_map, menu);
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onNewIntent(Intent newIntent) {
		super.onNewIntent(newIntent);
	}

	@Override
	public void onRestart() {
		super.onRestart();
		resetOverlays();
	}

	private void resetOverlays() {
		this.mOverlay = null;
		this.mMapOverlays = null;
		this.initializeTourMap();
		this.populateOverlays(); // this activity is restarted when a new item
									// is added to List View.
									// thus, the overlays (including the new
									// item) are re-added to the map view.
		mMapOverlays.add(mOverlay);

		if (mMapOverlays.size() > 0)
			this.mMapOverlays.remove(0);
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	public boolean isLocationDisplay() {
		return super.isLocationDisplayed();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
